// axios 封装模块
import axios from 'axios'
import store from '@/store'
import jsonBig from 'json-bigint'

var json = '{ "value" : 9223372036854775807, "v2": 123 }'
console.log(jsonBig.parse(json))

const request = axios.create({
  baseURL: 'http://toutiao-app.itheima.net/', // 基础路径
  // 自定义后端返回的原始数据
  transformResponse: [function (data) {
    try {
      // 如果转换成功则返回转换的数据结果
      return jsonBig.parse(data)
    } catch (err) {
      // 如果转换失败，则包装为统一数据格式并返回
      return {
        data
      }
    }
  }]
})
// 请求拦截器
request.interceptors.request.use(config => {
  // Do something before request is sent
  if (store.state.user) {
    config.headers.Authorization = `Bearer ${store.state.user.token}`
  }
  return config
}, error => {
  // Do something with request error
  return Promise.reject(error)
})
export default request
